package com.bro.person.admin.utils;

import com.bro.person.admin.model.AdminResource;
import com.bro.person.admin.model.AdminUser;
import com.bro.person.admin.struct.AdminAuthorityStruct;
import com.bro.person.admin.struct.AdminMenuStruct;
import com.bro.person.admin.struct.AdminUserStruct;
import com.bro.person.admin.struct.SecurityPermissionStruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;

/**
 * bean的转换类
 * @author bruce
 *
 */
public class AuthorityConvertUtil {
	
	private static final Logger logger = LoggerFactory.getLogger(AuthorityConvertUtil.class);

	/**
	 * 
	 * @param pojo
	 * @return
	 */
	public static AdminAuthorityStruct convert2AuthorityStruct(AdminResource pojo){
		logger.trace("[AuthorityConvertUtil][AdminAuthorityStruct]entering, pojo:{}", pojo);
		if(pojo!=null){
			AdminAuthorityStruct struct = new AdminAuthorityStruct();
			struct.setId(pojo.getId());
			struct.setTitle(pojo.getResourceName());
			struct.setCode(pojo.getCode());
			struct.setLink(pojo.getUrl());
			struct.setNavMenu(pojo.getNavMenu());
			long timeMillis = pojo.getCreateTime().getTime();
			struct.setCreateTimeMillis(timeMillis); 
			return struct;
		}
		return null;
	}
	
	/**
	 * 
	 * @param pojo
	 * @return
	 */
	public static AdminMenuStruct convert2AuthorityMenuStruct(AdminResource pojo){
		logger.trace("[AuthorityConvertUtil][AdminMenuStruct]entering, pojo:{}", pojo);
		if(pojo!=null && pojo.getNavMenu()!=null && pojo.getNavMenu()==1){
			AdminMenuStruct struct = new AdminMenuStruct();
			struct.setId(pojo.getId());
			struct.setTitle(pojo.getResourceName());
			struct.setCode(pojo.getCode());
			struct.setLink(pojo.getUrl());
			struct.setNavMenu(pojo.getNavMenu());
			
			//父菜单
			struct.setParentId(pojo.getParentId());
			if(pojo.getSort()==null){
				pojo.setSort(0);
			}
			struct.setSort(pojo.getSort());
			
			long timeMillis = pojo.getCreateTime().getTime();
			struct.setCreateTimeMillis(timeMillis); 
			return struct;
		}
		return null;
	}



	/**
	 *
	 * @param AdminMenuStructList
	 * @return
	 */
	public static List<AdminMenuStruct> convert2AuthorityMenuStruct(List<AdminResource> AdminMenuStructList){
		logger.trace("[AuthorityConvertUtil][convert2AuthorityMenuStruct] entering AdminMenuStructList:{}",AdminMenuStructList);
		//装换成 AdminMenuStruct
		Set<AdminMenuStruct> pojoSet = new HashSet<>();
		if(AdminMenuStructList!=null && AdminMenuStructList.size()>0) {
			for (AdminResource AdminResource : AdminMenuStructList){
				AdminMenuStruct AdminMenuStruct = convert2AuthorityMenuStruct(AdminResource);
				if(AdminMenuStruct!=null){
					pojoSet.add(AdminMenuStruct);
				}
			}
		}

		Set<AdminMenuStruct> AdminMenuStructSet = new TreeSet<>();
		if(pojoSet!=null && pojoSet.size()>0){
			//1. 取出顶级菜单
			Iterator<AdminMenuStruct> iterator = pojoSet.iterator();
			while (iterator.hasNext()){
				AdminMenuStruct pojo = iterator.next();
				if(pojo.getParentId()==0){
					AdminMenuStructSet.add(pojo);
					//删除无用已经处理过后的 对象
					iterator.remove();
				}
			}
            //2. 递归获取对应的菜单
            Set<Integer> pojoParentIdSet = pojoSet.stream().map(AdminMenuStruct::getParentId).collect(Collectors.toSet());
            for (AdminMenuStruct AdminMenuStruct : AdminMenuStructSet) {
				recursionMenuSet(AdminMenuStruct,pojoSet,pojoParentIdSet);
			}
		}
		return new ArrayList<>(AdminMenuStructSet);
	}

    /**
     * 递归获取某个顶级菜单下的所有子菜单
     * @param AdminMenuStruct
     * @param pojoSet
     * @param pojoParentIdSet
     */
	public static void recursionMenuSet(AdminMenuStruct AdminMenuStruct, Set<AdminMenuStruct> pojoSet, Set<Integer> pojoParentIdSet){
		Set<AdminMenuStruct> menus = new TreeSet<>();

		Iterator<AdminMenuStruct> iterator = pojoSet.iterator();
		while (iterator.hasNext()) {
			AdminMenuStruct pojo = iterator.next();
			if(pojo.getParentId().equals(AdminMenuStruct.getId())){
				menus.add(pojo);
				//iterator.remove();
				//判断是否有对应的子节点 如果有 继续遍历 如果没有直接 return
				if(pojoParentIdSet!=null && pojoParentIdSet.size()>0 && pojoParentIdSet.contains(pojo.getId())){
					recursionMenuSet(pojo,pojoSet,pojoParentIdSet);
				}
			}
		}
		if(menus.size()>0){
            AdminMenuStruct.setMenus(menus);
        }
	}

	
	/**
	 * 
	 * @param pojo
	 * @return
	 */
	public static AdminUserStruct convert2UserStruct(AdminUser pojo){
		logger.trace("[AuthorityConvertUtil][convert2UserStruct]entering, pojo:{}", pojo);
		if(pojo!=null){
			AdminUserStruct struct = new AdminUserStruct();
			struct.setUserId(pojo.getId());
			struct.setUsername(pojo.getUsername());
			struct.setNickname(pojo.getNickname());

			Date  createTime = pojo.getCreateTime();
			if(createTime!=null){
				struct.setCreateTimeMillis(createTime.getTime());
			}
			
			Date lastLoginTime = pojo.getLastLoginTime();
			if(lastLoginTime!=null){
				struct.setLastLoginTimeMillis(lastLoginTime.getTime());
			}
			return struct;
		}
		return null;
	}

	/**
	 * 转换对应的 permissionStruct
	 *
	 * @param pojo
	 * @return
	 */
	public static SecurityPermissionStruct convert2PermissionStruct(AdminResource pojo){
		logger.trace("[AuthorityConvertUtil][convert2PermissionStruct]entering, pojo:{}", pojo);
		if(pojo!=null){
			SecurityPermissionStruct struct = new SecurityPermissionStruct();
			struct.setPermissionName(pojo.getResourceName());
			struct.setPermissionCode(pojo.getCode());
			return struct;
		}
		return null;
	}

	/**
	 * 转换对应的 permissionStructList
	 *
	 * @param pojoList
	 * @return
	 */
	public static List<SecurityPermissionStruct> convert2PermissionStructList(List<AdminResource> pojoList){
		logger.trace("[AuthorityConvertUtil][convert2PermissionStructList]entering, pojoList:{}", pojoList);
		List<SecurityPermissionStruct> securityPermissionStructList = new ArrayList<>();
		if(pojoList!=null){
			for (AdminResource pojo : pojoList) {
				//为了防止 上层判断null 过滤错误的 空对象
				if(pojo!=null){
					SecurityPermissionStruct securityPermissionStruct = convert2PermissionStruct(pojo);
					securityPermissionStructList.add(securityPermissionStruct);
				}
			}
		}
		logger.info("[AuthorityConvertUtil][convert2PermissionStructList] 转换数据 securityPermissionStructList:{}",securityPermissionStructList);
		return securityPermissionStructList;
	}
}
